/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.service.CustomerService;
import com.bifrost.util.HttpContextUtils;
import com.bifrost.wechat.entity.WechatAccount;
import com.bifrost.wechat.service.WechatAccountService;
import com.bifrost.wechat.service.WxServiceContext;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 *初始化控制器
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/4/12 19:17
 */
@Controller
@RequestMapping("/{store}")
public class ProxyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProxyController.class);
    @Value("${wechat.oauth2.redirect.url:#{null}}")
    private String redirect_url;
    @Autowired
    private WxServiceContext wxServiceContext;
    @Autowired
    private CustomerService customerService;
    @Autowired
    WechatAccountService wechatAccountService;

    @GetMapping ("/proxy")
    public String redirect(@PathVariable("store") Long store, @RequestParam String url){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        HttpSession session = request.getSession(true);
        session.invalidate();
        if(store!=null && StringUtils.isNotBlank(url)){
           WechatAccount account = wechatAccountService.getAccount(store);
           if(account!=null && account.getAppId()!=null){
               String appId = account.getAppId();
               WxMpService wxService = wxServiceContext.getWxServiceByAppId(appId);
               String redirectedUrl = null;
               try {
                   redirectedUrl = wxService.oauth2buildAuthorizationUrl(String.format("%s/%s",redirect_url,appId), "snsapi_userinfo",String.format("/index.html%s%s","%3F%23", URLEncoder.encode(url,"utf-8")));
               } catch (UnsupportedEncodingException e) {
                   return "redirect:"+redirectedUrl;
               }
               return "redirect:"+redirectedUrl;
           }
        }
        return  null;
    }
}
